<template>
	<view>
		<view class="sticky">
			<my-search @click="clickSelect"></my-search>
		</view>
		<!-- 轮播图区域 -->
		<swiper :indicator-dots="true" :autoplay="true" :interval="5000" :duration="1000" circular>
			<swiper-item v-for="item in swiperList" :key="item.goods_id">
				<navigator class="swiper-item" :url="'/subpkg/goods_details/goods_details?goods_id=' + item.goods_id">
					<image :src="item.image_src"></image>
				</navigator>
			</swiper-item>
		</swiper>

		<!-- 分类导航区域 -->
		<view class="nav-list">
			<view class="nav-list-item" v-for="item in navList" :key="item.name" @click="navClickHandler(item)">
				<image :src="item.image_src"></image>
			</view>
		</view>

		<!-- 底部楼层区域 -->
		<view class="floor-list">
			<view class="floor-list-item" v-for="(item, i) in floorList" :key="i">
				<!-- 头部 -->
				<view class="floor-list-item-header">
					<image :src="item.floor_title.image_src" class="header-title" mode="widthFix"></image>
				</view>
				<!-- 主体区域 -->
				<view class="floor-list-item-main">
					<!-- 主体区域内容左侧 -->
					<navigator class="floor-list-item-main-left" :url="item.product_list[0].url">
						<image :src="item.product_list[0].image_src"
							:style="{width: item.product_list[0].image_width + 'rpx'}" mode="widthFix"></image>
					</navigator>
					<view class="floor-list-item-main-right">
						<navigator v-for="(item2, i) in item.product_list" :key="item2.name" v-if="i !== 0"
							:url="item2.url">
							<image :src="item2.image_src" :style="{width: item2.image_width + 'rpx'}" mode="widthFix">
							</image>
						</navigator>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import badgeMix from '@/mixins/tabbar-badge.js'
	export default {
		mixins: [badgeMix],
		data() {
			return {
				// 轮播图的数据列表
				swiperList: [],
				// 导航分类数据列表
				navList: [],
				// 楼层数据列表
				floorList: []
			};
		},
		onLoad() {
			// 获取轮播图列表
			this.getSwiperList();
			// 获取导航分类数据列表
			this.getNavList()
			// 获取楼层数据列表
			this.getFloorList()
		},
		methods: {
			// 获取轮播图列表
			async getSwiperList() {
				const {
					data: res
				} = await uni.$http.get(
					"/api/public/v1/home/swiperdata"
				);

				console.log(res, '1111111')

				if (res.meta.status !== 200) uni.$showMsg()

				this.swiperList = res.message
			},
			// 获取分类导航区域
			async getNavList() {
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/home/catitems')

				if (res.meta.status !== 200) uni.$showMsg()

				this.navList = res.message
			},
			// 获取楼层数据列表
			async getFloorList() {
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/home/floordata')
				if (res.meta.status !== 200) uni.$showMsg()

				res.message.forEach((floor) => {
					floor.product_list.forEach(prod => {
						prod.url = '/subpkg/goods_list/goods_list?' + prod.navigator_url.split('?')[1]
					})
				})

				this.floorList = res.message
			},
			// 点击分类导航区域的某一下项
			navClickHandler(item) {
				if (item.name === '分类') {
					uni.switchTab({
						url: '/pages/cate/cate'
					})
				}
			},
			// 点击搜索
			clickSelect() {
				uni.navigateTo({
					url: '/subpkg/search/search'
				})
			}
		},
	};
</script>

<style lang="scss">
	.sticky {
		position: sticky;
		top: 0;
		z-index: 9999;
	}

	swiper {
		height: 330rpx;

		.swiper-item,
		image {
			width: 100%;
			height: 100%;
		}
	}

	.nav-list {
		display: flex;
		justify-content: space-around;
		margin: 15px 0;

		.nav-list-item image {
			width: 128rpx;
			height: 140rpx;
		}
	}

	.floor-list {
		.floor-list-item {
			.floor-list-item-header {
				.header-title {
					width: 100%;
				}
			}

			.floor-list-item-main {
				display: flex;
				padding-left: 10rpx;

				.floor-list-item-main-right {
					display: flex;
					flex-wrap: wrap;
					justify-content: space-around;
				}
			}
		}

	}
</style>
